<template>
  <el-dialog
    v-if="chartOptionSettingDialog"
    v-model="chartOptionSettingDialog"
    :title="title"
    width="380px"
    top="8.4vh"
    append-to-body
    destroy-on-close
    :close-on-click-modal="false"
    :class="`${customClass} ${visualThemeClassName} ${xvueNameSpace}`"
  >
    <div
      class="chart-option-setting-main"
      :class="xvueTheme"
    >
      <PanelLabelOverflowTooltip
        ref="panelLabelOverflowTooltip"
        :component-style="form.componentStyle"
      />
      <el-form
        ref="form"
        v-panel-label-overflow-tooltip="getPanelLabelOverflowTooltip()"
        class="prop-form"
        label-position="right"
        :model="form"
        :rules="rules"
        label-width="85px"
      >
        <VsFormItem
          :label="$demoi18n('panel.commonX.chartType')"
          required
        >
          <el-select
            v-model="chartOption.chartType"
            class="chartType-select"
            :class="'panel-data-select-' + xvueTheme"
            :popper-class="'select-poper-' + xvueTheme"
            placeholder
            size="default"
            @change="optionChange"
          >
            <el-option
              v-for="item in chartTypeOption"
              :key="item.id"
              class="panel-data-select-option"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </VsFormItem>
        <BaseOption
          v-if="hasProperty('barWidth')"
          :form="form"
          option="barWidth"
        />
        <BaseOption
          v-if="hasProperty('arrangement')"
          :form="form"
          option="arrangement"
          @change="initColor"
        />
        <BaseOption
          v-if="hasProperty('lineType')"
          :form="form"
          option="lineType"
        />
        <BaseOption
          v-if="hasProperty('shadow')"
          :form="form"
          option="shadow"
        />
        <BaseOption
          v-if="hasProperty('yAxisLabel')"
          :form="form"
          option="yAxisLabel"
        />
        <BaseOption
          v-if="hasProperty('symbolSizeMax')"
          :form="form"
          option="symbolSizeMax"
        />
        <BaseOption
          v-if="hasProperty('symbolSizeMin')"
          :form="form"
          option="symbolSizeMin"
        />
        <BaseOption
          v-if="hasProperty('yAxisLabel')"
          :form="form"
          option="splitLine"
        />
        <BaseOption
          :form="form"
          option="echart.__.splitLine.show"
        />
        <BaseOption
          :form="form"
          option="echart.yAxis.splitNumber"
        />
        <BaseOption
          v-if="hasProperty('legend')"
          :form="form"
          option="legend"
        />
        <BaseOption
          :form="form"
          option="color"
          :config="config"
          :color-group="getColorGroup()"
        />

        <!-- 折线和柱形 参考值 -->
        <VsFormItem
          v-if="hasProperty('isMarklineShow')"
          :label="$demoi18n('word.referenceValue')"
        >
          <el-switch v-model="form.hasMarkLine"></el-switch>
        </VsFormItem>
        <VsFormItem
          v-for="(item, index) in form.markLine"
          v-show="hasProperty('isMarklineShow') && form.hasMarkLine"
          :key="index"
          :label="`${$demoi18n('word.referenceLine')}${index + 1}`"
        >
          <template #default="scope">
            <el-input-number
              v-model="item.value"
              class="value"
              :style="{ width: 'calc(100% - 70px)', verticalAlign: 'middle' }"
              :placeholder="$demoi18n('panel.label.referencePlaceholder')"
              size="default"
              controls-position="right"
            ></el-input-number>
            <el-color-picker
              v-model="item.color"
              :popper-class="'vis-color-picker-panel-' + xvueTheme"
              :style="{ verticalAlign: 'middle', marginLeft: '12px' }"
              color-format="hex"
              size="default"
            ></el-color-picker>

            <el-button
              icon="Delete"
              :link="true"
              type="primary"
              :disabled="delDisable"
              :style="{ verticalAlign: 'middle', marginLeft: '12px' }"
              @click.stop="deleteCurRow(scope.row)"
            ></el-button>
          </template>
        </VsFormItem>
        <div v-show="hasProperty('isMarklineShow') && form.hasMarkLine">
          <el-button
            class="btn-sure"
            size="default"
            icon="CirclePlus"
            @click.stop="addNewMarkLine()"
          >
            {{ $demoi18n('btn.add') }}
          </el-button>
        </div>
      </el-form>
      <div class="action-button-group">
        <el-button
          class="button-sure btn-sure"
          size="small"
          type="primary"
          @click="save()"
        >
          {{ $demoi18n('btn.confirm') }}
        </el-button>
        <el-button
          class="button-cancel"
          size="small"
          @click="closeDialog()"
        >
          {{ $demoi18n('btn.cancel') }}
        </el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import BaseOption from '@/components/common/base-option/base-option.vue';
import PanelLabelOverflowTooltip from '@/components/common/overflow-tooltip/overflow-tooltip.vue';
import { generateRules, genRequireRule } from '@/utils/rules';
import { objHas } from '@/utils/util';
import colorGroup from './colorGroup.js';
import defaultOption from './defaultOption';

export default {
  name: 'ChartOptionSetting',
  components: { BaseOption, PanelLabelOverflowTooltip },
  props: ['componentStyle'],
  emits: ['save'],
  data() {
    return {
      xvueNameSpace: process.env.xvueNameSpace, // 给挂载在body下的元素加上主类名
      colorGroup: colorGroup,
      title: this.$demoi18n('panel.commonX.advancedSetting.title'),
      customClass: `chart-option-setting chart-option-setting-${window._adoPremise.uc.theme || 'light'}`,
      xvueTheme: window._adoPremise.uc.theme, // 'dark',
      chartOptionSettingDialog: false,
      scope: null, // 选中的行的配置
      chartOption: null, // 拷贝的配置
      form: null, // 拷贝的配置的option属性
      chartTypeOption: [
        {
          label: this.$demoi18n('word.echart.bar'),
          value: 'uniaxial-bar',
        },
        {
          label: this.$demoi18n('word.echart.line'),
          value: 'uniaxial-line',
        },
        {
          label: this.$demoi18n('word.echart.scatter'),
          value: 'scatter',
        },
      ],
      delDisable: true,
      rules: {
        barWidth: genRequireRule({
          type: 'number',
          label: this.$demoi18n('label.BarWith'),
          trigger: ['blur', 'change'],
        }),
        symbolSizeMax: genRequireRule({
          type: 'number',
          label: this.$demoi18n('panel.label.maxScatter'),
          trigger: ['blur', 'change'],
        }),
        symbolSizeMin: genRequireRule({
          type: 'number',
          label: this.$demoi18n('panel.label.minScatter'),
          trigger: ['blur', 'change'],
        }),
        'echart.yAxis.splitNumber': genRequireRule({
          type: 'number',
          label: this.$demoi18n('panel.label.yAxisSplitNumber'),
          trigger: ['blur', 'change'],
        }),
      },
    };
  },
  watch: {},
  methods: {
    genRequireRule,
    generateRules,
    getPanelLabelOverflowTooltip() {
      return {
        parentComp: this,
      };
    },
    save() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return;
        }

        this.chartOption.option = this.form;
        this.scope.row = this.chartOption;
        this.$emit('save', this.scope);
        this.chartOptionSettingDialog = false;
      });
    },
    openDialog(scope) {
      this.scope = scope;
      this.chartOption = JSON.parse(JSON.stringify(scope.row));
      this.form = this.chartOption.option;
      this.form.componentStyle = this.componentStyle;

      const keys = Object.keys(this.colorGroup[this.componentStyle][this.chartOption.chartType]);

      // 切换风格后配置项的color不在列表中，重置颜色
      if (!keys.includes(this.form.color)) {
        this.initColor();
      }
      this.delDisable = this.form.markLine.length <= 1;
      this.chartOptionSettingDialog = true;
    },
    closeDialog() {
      this.chartOptionSettingDialog = false;
    },
    optionChange(val) {
      this.$refs.form.clearValidate();
      this.form = JSON.parse(JSON.stringify(defaultOption[val]));
      this.initColor();
    },
    initColor() {
      // 空函数，防止报错
    },
    addNewMarkLine() {
      const markLineOption = JSON.parse(JSON.stringify(defaultOption[this.chartOption.chartType].markLine[0]));
      this.form.markLine.push(markLineOption);
      const count = this.form.markLine.length;
      if (count > 1) {
        this.delDisable = false;
      }
    },
    deleteCurRow(item) {
      const index = this.form.markLine.indexOf(item);
      this.form.markLine.splice(index, 1);
      const count = this.form.markLine.length;
      if (count <= 1) {
        this.delDisable = true;
      }
    },
    hasProperty(key) {
      const { chartType } = this.chartOption;
      const conf = defaultOption[chartType];
      return objHas(conf, key);
    },
    getColorGroup() {
      const { chartType } = this.chartOption;
      return {
        light: this.colorGroup.light[chartType],
        dark: this.colorGroup.dark[chartType],
      };
    },
  },
};
</script>

<style lang="less">
@import '../../../common/color-picker-panel.less';
</style>
<style lang="less">
.chart-option-setting {
  .el-dialog__header {
    padding: 10px 20px;
    .el-dialog__headerbtn {
      top: 15px;
    }
  }
  :deep(.el-dialog__body) {
    padding-top: 10px;
    .chart-option-setting-main {
      .prop-form {
        .el-form-item {
          margin-bottom: 0;
          .el-form-item__label {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
        .el-form-item.is-error {
          margin-bottom: 15px;
        }
        .color-radio-group {
          background-color: #eeeeee;
          :deep(.el-radio-button__inner) {
            display: inline-block;
            padding: 2px 2px;
            border: 0;
            border-radius: 0;
            height: 24px;
            margin: 2px 2px;
            background: #eeeeee;
          }
          :deep(.is-active) {
            .el-radio-button__inner {
              background-color: #409eff;
            }
          }
        }
      }
      .action-button-group {
        text-align: center;
        padding-top: 20px;
        .button-sure {
          padding: 9px 35px;
        }
        .button-cancel {
          padding: 9px 35px;
        }
      }
    }
  }
}

@font_color_dark: #98cae6;
@font_color_active_dark: #00f0ff;
@background_color_dark: #071f30;
@bgColor_dark: #000c17;
@borderColor_dark: #116584;
@textColor_dark: #ffffff;

.chart-option-setting-dark {
  .el-dialog__header {
    background: #183c5a;
    .el-dialog__title {
      font-size: 16px;
      color: #fff;
    }

    .el-dialog__headerbtn .el-dialog__close {
      color: #fff;
    }
  }
  :deep(.el-dialog__body) {
    color: #606266;
    background: #0e2b40;

    .chart-option-setting-main {
      background: #0e2b40;
      .el-form-item__label {
        color: @font_color_dark;
      }
      .el-form-item__content {
        .el-input {
          .el-input__inner {
            border-color: @borderColor_dark;
            color: @textColor_dark;
            background-color: @bgColor_dark;
          }

          .el-input__icon {
            color: @borderColor_dark;
          }
        }

        .el-radio-group {
          .el-radio {
            color: @textColor_dark;

            .el-radio__input {
              .el-radio__inner {
                border-color: @font_color_active_dark;
                background-color: unset;
              }
            }

            .is-checked {
              .el-radio__inner {
                &::after {
                  background-color: @font_color_active_dark;
                }
              }
            }

            .el-radio__label {
              color: #fff;
            }
          }
          .is-checked {
            .el-radio__label {
              color: @font_color_active_dark;
            }
          }
        }

        .el-checkbox {
          .is-checked {
            .el-checkbox__inner {
              background-color: @font_color_active_dark;
              border-color: @font_color_active_dark;
            }
          }
          .el-checkbox__label {
            color: #fff;
          }
        }

        .el-checkbox__input.is-checked + .el-checkbox__label {
          color: @font_color_active_dark;
        }

        .input-number {
          .el-input-number__decrease {
            background-color: #000c17;
            border-color: #116584;
            color: #116584;
          }

          .el-input-number__increase {
            background-color: #000c17;
            border-color: #116584;
            color: #116584;
          }
        }

        .slider-number {
          color: @textColor_dark;
        }

        .slider-number-left {
          color: @textColor_dark;
        }

        .slider-number-right {
          color: @textColor_dark;
        }

        .el-switch__core {
          background-color: #064559;
          border-color: #064559;
        }

        .el-switch.is-checked {
          .el-switch__core {
            background-color: #0eb4c2;
            border-color: #0eb4c2;
          }
        }

        .color-radio-group {
          background-color: #064559;
          .el-radio-button {
            background-color: #064559;
            .el-radio-button__inner {
              background-color: #064559;
            }
          }

          :deep(.is-active) {
            .el-radio-button__inner {
              background-color: #409eff !important;
            }
          }
        }
        .el-color-picker {
          .el-color-picker__trigger {
            border-color: @borderColor_dark;
          }
        }
      }

      // 默认按钮样式类
      .btn-sure {
        background-color: #0eb4c2;
        border-color: #0eb4c2;
        color: #fff;

        .el-icon-circle-plus {
          color: #fff;
        }
      }

      .button-cancel {
        background-color: #064559;
        border-color: #064559;
        color: #fff;

        .el-icon-circle-plus {
          color: #fff;
        }
      }
    }
  }
}
</style>
